//
//  ThirdViewController.m
//  MyAppDemo
//
//  Created by 千锋 on 15/12/5.
//  Copyright (c) 2015年 Aike. All rights reserved.
//

#import "ThirdViewController.h"
#import <MAMapKit/MAMapKit.h>
//导入搜索高德地图


#import <AMapSearchKit/AMapSearchAPI.h>
#import <AMapSearchKit/AMapCommonObj.h>
#import <AMapSearchKit/AMapSearchObj.h>
#import <AMapLocationKit/AMapLocationKit.h>

@interface ThirdViewController ()<AMapSearchDelegate,MAMapViewDelegate,AMapLocationManagerDelegate,UIGestureRecognizerDelegate>
{
    MAMapView * _mapView;
    AMapSearchAPI * _search;
    AMapLocationManager * _locationManager;
}

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createMapView];
    [self startLocation];
    _mapView.showsUserLocation = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    [_mapView setZoomLevel:16.1 animated:YES];
}

//创建地图
-(void)createMapView{
    
    _mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    //添加手势
    UILongPressGestureRecognizer * longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onLong:)];
    [_mapView addGestureRecognizer:longGesture];
    //创建
    NSArray * mapTypeArray = @[@"标准",@"卫星",@"混合"];
    UISegmentedControl * sc = [[UISegmentedControl alloc]initWithItems:mapTypeArray];
    sc.frame = CGRectMake(20, 50, 200, 30);
    //添加响应
    [sc addTarget:self action:@selector(mapTypeChanged:) forControlEvents:UIControlEventValueChanged];
    [_mapView addSubview:sc];
    _mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    
}



//UISegmentedControl响应回调
-(void)mapTypeChanged:(UISegmentedControl *)sender{
    //获取当前值
    NSInteger index = sender.selectedSegmentIndex;
    //设置地图显示方式
    //    MKMapTypeStandard = 0,标准
    //    MKMapTypeSatellite,  卫星
    //    MKMapTypeHybrid  混合
    _mapView.mapType = index;
}
-(void)onLong:(UILongPressGestureRecognizer *)sender{
    //判断当前手势状态
    if (sender.state != UIGestureRecognizerStateBegan) {
        return;
    }
    //获取手势在VIEW上的位置
    CGPoint gesturePoint = [sender locationInView:_mapView];
    //将手势在UIView上的位置  转换为经纬度
    CLLocationCoordinate2D  coordinate2D = [_mapView convertPoint:gesturePoint toCoordinateFromView:_mapView];
    MAPointAnnotation * pointAnnotation = [[MAPointAnnotation alloc]init];
    //设置经纬度
    pointAnnotation.coordinate = coordinate2D;
    //设置标题
    pointAnnotation.title = @"我是大头针";
    //设置副标题
    pointAnnotation.subtitle = @"美女多多";
    //添加
    [_mapView addAnnotation:pointAnnotation];
    //创建覆盖物
    MACircle * circle = [MACircle circleWithCenterCoordinate:coordinate2D radius:10];
    //添加到地图上
    //创建添加物  回调创建覆盖物
    [_mapView addOverlay:circle];
    //获取当前大头针指向的经纬度所在的位置
    //进行地理编码和反编码
    CLGeocoder * geoCoder = [[CLGeocoder alloc]init];
    //反地理编码 ，将 一个经纬度转换为一个具体的地址名称
    CLLocation * location = [[CLLocation alloc]initWithLatitude:coordinate2D.latitude longitude:coordinate2D.longitude];
    //<#(CLLocation *)#>1. 经纬度对象  2.<#^(NSArray *placemarks, NSError *error)completionHandler#>反地理编码后回调的block
    [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        //1.placemarks 返回的地理位置信息 2. 错误信息
        CLPlacemark * placeMark = [placemarks firstObject];
        NSDictionary * address = placeMark.addressDictionary;
        NSArray * addressArray = address[@"FormattedAddressLines"];
        NSLog(@"地址 = %@",addressArray[0]);
        UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(52, 400, 300, 50)];
        [_mapView addSubview:lable];
        lable.text =addressArray[0];
    }];
    
    
}


-(void)startLocation{
    [_mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES];
    _mapView.pausesLocationUpdatesAutomatically = NO;
}

//-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
//   if (updatingLocation) {
//        NSLog(@"%f,%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
//   }
//
//}



- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    MAPinAnnotationView * annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
    if (!annotationView) {
        annotationView = [[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"PIN"];
        
    }
    annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
    annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
    annotationView.draggable = YES;        //设置标注可以拖动，默认为NO
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2_12.jpg"]];
    imageView.frame = CGRectMake(0, 0, 30, 30);
    annotationView.leftCalloutAccessoryView = imageView;
    //显示
    //设置右视图
    UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    lable.text = @"<<";
    annotationView.rightCalloutAccessoryView = lable;
    //设置大头针颜色
    //    MKPinAnnotationColorRed = 0,
    //    MKPinAnnotationColorGreen,
    //    MKPinAnnotationColorPurple
    annotationView.pinColor = MAPinAnnotationColorPurple;
    //设置镂空颜色
    annotationView.tintColor = [UIColor greenColor];
    
    return annotationView;
}
#pragma mark - 解决手势冲突
//可以同时使用多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
